//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var dingyue=require("../../common/dingyue")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    userType: "",
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uid = wx.getStorageSync('uid');
    var userType = wx.getStorageSync('userType');
    var isAlreadyPayPassword = wx.getStorageSync('isAlreadyPasswords');
    this.setData({
      uid: uid,
      userType: userType,
      isAlreadyPayPassword: isAlreadyPayPassword,
    })
    this.BusinessFreePayOrAllShopAllMaImgInfo();
  },

  //获取保证码图和需要交的保证码额度
  BusinessFreePayOrAllShopAllMaImgInfo: function () {
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BusinessFreePayOrAllShopAllMaImgInfo&UID=" + uid + "&OType=" + 2),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        var BottomImg = json.ResultData.BottomImg;
        var IsHavePower = json.ResultData.IsHavePower;
        var TopImg = json.ResultData.TopImg;
        var buttonImg = json.ResultData.buttonImg;
        var EnsureMa = json.ResultData.EnsureMa;
        var EventType = json.ResultData.EventType;
        console.log(json)
        that.setData({
          BottomImg: BottomImg,
          IsHavePower: IsHavePower,
          TopImg: TopImg,
          buttonImg: buttonImg,
          EnsureMa: EnsureMa,
          EventType: EventType,
        })
        wx.setStorageSync('EventType', json.ResultData.EventType);
      }
    })
  },
  // 接受协议
  agreeAndReceipt: function (e) {
    if (e.detail.value.length == 0) {
      this.setData({
        agreeAndReceipt: "0",
      })
    } else {
      this.setData({
        agreeAndReceipt: "1",
      })
    }
  },
  // 点击缴纳保证码按钮弹出密码支付
  popUp: function (e) {
    var that = this;
    var agreeAndReceipt = this.data.agreeAndReceipt;
    var showPayPwdInput = this.data.showPayPwdInput;
    var EventType = wx.getStorageSync('EventType');

    if (agreeAndReceipt != "1") {
      wx.showToast({
        title: "请同意并接受以下协议！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (EventType == '2') {
      wx.showToast({
        title: "您已缴纳保证码！",
        icon: 'none',
        duration: 2000
      });
    }
    if (EventType != '2') {
      that.setData({
        showPayPwdInput: true,
      })
    }

  },

  // 确定  0交保证码，1补齐保证码，2已缴纳保证码
  submit: function (e) {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var EventType = wx.getStorageSync('EventType');
    console.log(EventType)
    // 支付密码是否正确
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showLoading({
            title: '加载中',
          })
          if (EventType == '0') {
            wx.request({
              url: baseJs.api_User + "BusinessBorrowMaToPayEnsureMa" + "?" + "UID=" + uid,
              success: function (res) {
                var json = res.data;
                console.log(json)
                if (json.ResultFlag == 1) {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });

                  //缴纳保证码 成功，发送信息
                  var tmplIds = ['fCfE4sxDwCJm9IetQoNhaROAM1SLngHCZBokUWsYwRI']
                  wx.requestSubscribeMessage({
                    tmplIds: tmplIds,
                    success(res) {
                      console.log(res);
                      dingyue.payMa()
                    },
                    fail: err => {
                      console.log(err);
                    }
                  })
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1000)
                } else {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });
                }
                wx.hideLoading();
              }
            })
          } else if (EventType == '1') {
            wx.request({
              url: baseJs.api_User + "BusinessAddSupplyEnsureMa" + "?" + "UID=" + uid,
              success: function (res) {
                var json = res.data;
                console.log(json)
                if (json.ResultFlag == 1) {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });

                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1000)
                } else {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });
                }
                wx.hideLoading();
              }
            })
          }
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },

  //显示支付密码输入层
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true,
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: '',
    });
  },
  // 获取焦点
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function (e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
})